package com.hongyue.Dey01;

import java.util.Objects;

public class ChuZi extends Thread {
    private BaoZi baozi;
    @Override
    public void run() {
        int i = 0;
        while (true) {
            synchronized (baozi) {
                if (BaoZi.getBaozi()) {
                    try {
                        baozi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("厨师"+getName()+"开始制作包子");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (i % 2 == 0) {
                    BaoZi.setPi("水晶");
                    BaoZi.setXian("莲蓉");
                    System.out.println("厨师" + getName() + "制作好了"+BaoZi.getPi()+BaoZi.getXian()+"包");
                } else {
                    BaoZi.setPi("韭菜");
                    BaoZi.setXian("鸡蛋");
                    System.out.println("厨师" + getName() + "制作好了"+BaoZi.getPi()+BaoZi.getXian()+"包");
                }
                i++;
                BaoZi.setBaozi(true);
                baozi.notifyAll();
            }
        }
    }

    public ChuZi() {
    }

    public ChuZi(String name, BaoZi baozi) {
        super(name);
        this.baozi = baozi;
    }

    public BaoZi getBaozi() {
        return baozi;
    }

    public void setBaozi(BaoZi baozi) {
        this.baozi = baozi;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ChuZi chuZi = (ChuZi) o;
        return Objects.equals(baozi, chuZi.baozi);
    }

    @Override
    public int hashCode() {
        return Objects.hash(baozi);
    }

    @Override
    public String toString() {
        return "ChuZi{" +
                "baozi='" + baozi + '\'' +
                '}';
    }
}
